package com.huike.business.service.impl;


import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
@Autowired
private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;
    /**
     * 添加商机跟进
     *
     * @param businessTrackVo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addBusinessTrackRecord(BusinessTrackVo businessTrackVo) {
        //1、修改商户基本信息
        tbBusinessTrackRecordMapper.updateBusiness(businessTrackVo);
        //2、新增商机跟进

        businessTrackVo.setCreateBy(SecurityUtils.getUsername());
        tbBusinessTrackRecordMapper.addBusinessTrackRecord(businessTrackVo);

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<TbBusinessTrackRecord> listRecords(Long id) {
        List<TbBusinessTrackRecord> trackRecords = tbBusinessTrackRecordMapper.listRecords(id);
        trackRecords.forEach(trackRecord -> {
            String keyItems = trackRecord.getKeyItems();
            List<String> keys = new ArrayList<>();
            if (keyItems.contains(",")){
                //截取
                String[] split = keyItems.split(",");
                //遍历
                for (String s : split) {
                    //查询keys
                    String key = tbBusinessTrackRecordMapper.getKays(s);
                    keys.add(key);
                }
            }else {
                String kay = tbBusinessTrackRecordMapper.getKays(keyItems);
                keys.add(kay);
            }
            trackRecord.setKeys(keys);
        });
        return trackRecords;
    }
}
